import historyStore from '@/store/history';

import Message from 'tdesign-miniprogram/message/index';
const messageDefaultConfig = (content) => {
    return {
        context: this,
        offset: [60, 32],
        content: content
    };
};

const tableColumns = [
    { title: 'date', key: 'date' },
    { title: 'T_max', key: 'T_max' },
    { title: 'T_min', key: 'T_min' },
    { title: 'P', key: 'P' },
    { title: 'U', key: 'U' },
    { title: 'DD', key: 'DD' },
    { title: 'Ff', key: 'Ff' }
];
let filePath;

Page({
    data: {
        tableColumns,
        tableData: []
    },

    onLoad(options) {
        filePath = options.path;
    },

    onReady() {
        const found = historyStore.getFileData(filePath);
        if (found) {
            this.setData({
                tableData: found
            });
            return;
        }
        const that = this;
        wx.showLoading({
            title: '加载中',
            mask: true
        });
        wx.request({
            url: filePath,
            headers: {
                'Content-Type': 'application/json'
            },
            success(res) {
                that.setData({
                    tableData: res.data
                });
                historyStore.addFileData(filePath, res.data);
            },
            fail(err) {
                Message.error(messageDefaultConfig('请求数据失败'));
            },
            complete() {
                wx.hideLoading();
            }
        });
    }
});
